package org.jianfa.learn.dubbo.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

import javax.servlet.ServletRequest;

@Activate(group = {CommonConstants.CONSUMER})
public class TransportIpFilter implements Filter {

    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        try {
            ServletRequest servletRequest = RequestContext.REQUEST_LOCAL.get();
            String remoteAddr = servletRequest.getRemoteAddr();
            RpcContext.getContext().setAttachment("remoteAddr", remoteAddr);
            return invoker.invoke(invocation);
        } finally {
            RequestContext.REQUEST_LOCAL.remove();
        }
    }
}
